import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:screen_brightness/screen_brightness.dart';
import '../models/scene_model.dart';

class LightProvider with ChangeNotifier {
  bool _isOn = false;
  bool _showControls = true;
  double _brightness = 1.0;
  double _colorTemperature = 0.5;
  String _currentMode = 'standard';
  double _hue = 0.0;
  double _saturation = 0.0;
  double _screenBrightness = 1.0;
  bool _showUI = true;  // 新增：控制整体UI的显示隐藏

  // 预设的初始颜色（柔和的暖白色）
  static const Color defaultColor = Color(0xFFFFF5E6);

  LightProvider() {
    _initializeSettings();
  }

  Future<void> _initializeSettings() async {
    // 将默认颜色转换为HSV
    final HSVColor hsvColor = HSVColor.fromColor(defaultColor);

    // 设置HSV值
    _hue = hsvColor.hue;
    _saturation = hsvColor.saturation;
    _brightness = hsvColor.value;

    // 获取并设置当前系统屏幕亮度
    try {
      _screenBrightness = await ScreenBrightness().current;
    } catch (e) {
      print('Failed to get current brightness: $e');
      _screenBrightness = 1.0; // 如果获取失败，使用默认值
    }

    notifyListeners();
  }

  bool get isOn => _isOn;
  bool get showControls => _showControls;
  double get brightness => _brightness;
  double get colorTemperature => _colorTemperature;
  String get currentMode => _currentMode;
  double get hue => _hue;
  double get saturation => _saturation;
  double get screenBrightness => _screenBrightness;
  bool get showUI => _showUI;  // 新增：getter

  void togglePower() {
    _isOn = !_isOn;
    notifyListeners();
  }

  void toggleControlsVisibility() {
    _showControls = !_showControls;
    notifyListeners();
  }

  void setBrightness(double value) {
    if (value >= 0 && value <= 1) {
      _brightness = value;
      notifyListeners();
    }
  }

  void setColorTemperature(double value) {
    if (value >= 0 && value <= 1) {
      _colorTemperature = value;
      notifyListeners();
    }
  }

  void setMode(String mode) {
    _currentMode = mode;
    switch (mode) {
      case 'standard':
        _brightness = 0.5;
        _colorTemperature = 0.5;
        break;
      case 'selfie':
        _brightness = 0.7;
        _colorTemperature = 0.6;
        break;
      case 'video':
        _brightness = 0.8;
        _colorTemperature = 0.5;
        break;
      case 'professional':
      // Keep current settings
        break;
    }
    notifyListeners();
  }

  void setHue(double value) {
    _hue = value.clamp(0, 360);
    notifyListeners();
  }

  void setSaturation(double value) {
    _saturation = value.clamp(0, 1);
    notifyListeners();
  }

  Future<void> setScreenBrightness(double value) async {
    try {
      _screenBrightness = value;
      await ScreenBrightness().setScreenBrightness(value);
      notifyListeners();
    } catch (e) {
      print('Failed to set screen brightness: $e');
    }
  }

  // 应用场景
  void applyScene(LightScene scene) {
    _brightness = scene.brightness;
    _colorTemperature = scene.colorTemperature;
    if (!_isOn) {
      _isOn = true;
    }
    notifyListeners();
  }

  // 获取当前状态作为新场景
  LightScene getCurrentStateAsScene({
    required String name,
    required String description,
    required String iconName,
  }) {
    return LightScene(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      name: name,
      description: description,
      iconName: iconName,
      brightness: _brightness,
      colorTemperature: _colorTemperature,
      isCustom: true,
    );
  }

  // 重置所有值
  Future<void> reset() async {
    _isOn = false;
    _brightness = 0.5;
    _colorTemperature = 0.5;
    _currentMode = 'standard';
    _hue = 0;
    _saturation = 0.5;
    await setScreenBrightness(0.5);
    notifyListeners();
  }

  // 获取当前系统屏幕亮度
  Future<void> getCurrentBrightness() async {
    try {
      _screenBrightness = await ScreenBrightness().current;
      notifyListeners();
    } catch (e) {
      print('Failed to get current brightness: $e');
    }
  }

  // 新增：切换整体UI的显示隐藏
  void toggleUIVisibility() {
    _showUI = !_showUI;
    notifyListeners();
  }
}
